package com.xoa.service.equipment.impl;

import com.alibaba.fastjson.JSONArray;
import com.xoa.dao.equipment.LimsEquipEventPlanParaMapper;
import com.xoa.model.equipment.LimsEquipEventPlanPara;
import com.xoa.model.equipment.LimsEquipEventsCapy;
import com.xoa.service.equipment.LimsEquipEventPlanParaService;
import com.xoa.util.LimsJson;
 import com.xoa.util.page.PageParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @作者 杨岩林
 * @创建日期 10:492019/12/23
 * @类介绍
 */
@Service
public class LimsEquipEventPlanParaServiceImpl implements LimsEquipEventPlanParaService {

    @Autowired
    private LimsEquipEventPlanParaMapper limsEquipEventPlanParaMapper;
    @Override
    public LimsJson showByEventId(Integer eventId, Integer page, Integer limit) {
        LimsJson limsJson=new LimsJson(1,"err");
        Map map=new HashMap();
        PageParams pageParams=new PageParams();
        pageParams.setPage(page);
        pageParams.setPageSize(limit);
        map.put("page",pageParams);
        map.put("eventId",eventId);
        List<LimsEquipEventPlanPara> limsEquipEventPlanParaList=limsEquipEventPlanParaMapper.selectByEventId(map);
        if(limsEquipEventPlanParaList!=null && limsEquipEventPlanParaList.size()>0){
            limsJson.setCode(0);
            limsJson.setMsg("查询成功");
            limsJson.setData(limsEquipEventPlanParaList);
            limsJson.setCount(pageParams.getTotal());
        }else{
            limsJson.setCode(0);
            limsJson.setMsg("无数据");
        }
        return limsJson;
    }

    @Override
    public LimsJson deleteByIds(String ids) {
        LimsJson limsJson=new LimsJson(1,"err");
        String[] idArray=ids.split(",");
        int result=limsEquipEventPlanParaMapper.deleteByIds(idArray);
        if(result>0){
            limsJson.setCode(0);
            limsJson.setMsg("删除成功");
        }
        return limsJson;
    }

    @Override
    public LimsJson addOrUpdate(String json) {
        LimsJson limsJson=new LimsJson(1,"err");
        int a=0,b=0,c=0,d=0;
       /* JSONArray jsonarray = JSONArray.fromObject(json);
        List<LimsEquipEventPlanPara> limsEquipEventPlanParaList = (List<LimsEquipEventPlanPara>) JSONArray.toCollection(jsonarray, LimsEquipEventPlanPara.class);*/
        List<LimsEquipEventPlanPara> limsEquipEventPlanParaList= JSONArray.parseArray(json,LimsEquipEventPlanPara.class);
        for(LimsEquipEventPlanPara limsEquipEventPlanPara:limsEquipEventPlanParaList){
            if(limsEquipEventPlanPara.getPlanParaId()!=null){
                a++;
                int result=limsEquipEventPlanParaMapper.updateByPrimaryKeySelective(limsEquipEventPlanPara);
                if(result>0){
                    b++;
                }
            }else{
                c++;
                int result=limsEquipEventPlanParaMapper.insertSelective(limsEquipEventPlanPara);
                if(result>0){
                    d++;
                }
            }
        }
        if(a==b&& c==d){
            limsJson.setMsg("保存成功");
            limsJson.setCode(0);
        }
        return limsJson;
    }
}
